package com.dcy.znoa.service.conference.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.conference.model.conferenceRoom.SelectConferenceRoomModel;
import com.dcy.znoa.dao.conference.ConferencePersonDao;
import com.dcy.znoa.dao.conference.ConferenceRoomDao;
import com.dcy.znoa.domain.conference.ConferenceRoom;
import com.dcy.znoa.domain.conference.ConferenceRoomStatusVo;
import com.dcy.znoa.service.conference.ConferenceRoomService;

@Service
public class ConferenceRoomServiceImpl implements ConferenceRoomService {
	@Autowired
	ConferenceRoomDao conferenceRoomDao;
	
	@Autowired
	ConferencePersonDao conferencePersonDao;
	
	@Override
	public JsonForList selectList(SelectConferenceRoomModel model)throws InvocationTargetException, IllegalAccessException {
        List<ConferenceRoom> list;       
        
        EntityWrapper<ConferenceRoom> ew = new EntityWrapper<>();
        ew.setEntity(ConferenceRoom.builder().compId(model.getCompId()).build());
        ew.eq("valid_flag", model.getValidFlag());
        if (null != model.getConferName() && !model.getConferName().isEmpty()) {
        	ew.like("confer_name", model.getConferName());
		}
        Page<ConferenceRoom> page = new Page(model.getPageNum(), model.getPageSize());
        list = conferenceRoomDao.selectPage(page, ew);
        return new JsonForList(list, page.getTotal());
	}

	@Override
	public JsonCommon insert(ConferenceRoom conferenceRoom) {
		//查询：同一个公司下不允许有同名的会议室
		List list = conferenceRoomDao.selectList( new EntityWrapper<ConferenceRoom>()
				                                      .eq("comp_id",conferenceRoom.getCompId())
				                                      .eq("confer_name",conferenceRoom.getConferName())
				                                      .eq("valid_flag",1));
		if(!list.isEmpty()){
			return new JsonCommon(JsonCommon.INSERT_ERROR_CODE,"不允许有同名的会议室");
		}
        Integer result = conferenceRoomDao.insert(conferenceRoom);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonCommon updateById(ConferenceRoom conferenceRoom) {
		//查询：同一个公司下不允许有同名的会议室
		List list = conferenceRoomDao.selectList( new EntityWrapper<ConferenceRoom>()
				                                      .ne("confer_id", conferenceRoom.getConferId())
				                                      .eq("comp_id",conferenceRoom.getCompId())
				                                      .eq("confer_name",conferenceRoom.getConferName())
				                                      .eq("valid_flag",1));
		if(!list.isEmpty()){
			return new JsonCommon(JsonCommon.INSERT_ERROR_CODE,"不允许有同名的会议室");
		}
				
		Integer result = conferenceRoomDao.updateById(conferenceRoom);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonForList selectConferenceRoomStatus(SelectConferenceRoomModel model) {
		List<ConferenceRoomStatusVo> list = conferencePersonDao.selectConferenceRoomStatus(model);
		if (list.isEmpty()) {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		} else {
			return new JsonForList(list);
		}
	}



}
